<?php
namespace ZhuJiu\Controller;
use Think\Controller;
use LaneWeChat\Core\Wechat;
use LaneWeChat\Autoloader;
use LaneWeChat\Core\Menu;
use LaneWeChat\Core\UserManage;
use LaneWeChat\Core\Popularize;
class IndexController extends Controller {
    public function __construct(){
        require C('PUBLIC').'/Public/LaneWeChat/lanewechat.php';
    }
    public function index(){
        AutoLoader::register();
        $wechat = new WeChat(WECHAT_TOKEN, TRUE);
        echo $wechat->run();
    }
    public function getMenu(){
//        $menu = Menu::getMenu();
//        print(json_encode($menu));
        require C('PUBLIC').'/Public/data/MenuWeixin.class.php';
        $Menu = new \data\Menu\Menu();
        $menuList = $Menu->getMenu(C('WECHAT_APPID'),C('WECHAT_APPSECRET'));
    }
    public function setMenu(){
        $menu = I('menu','','');
        cookie('menu',$menu);
//         $menu = '{
//            "button":[
//                {
//                    "type":"view",
//                    "name":"比赛竞猜",
//                    "url":"http://www.baidu.com/"
//                },
//                {
//                    "type":"view",
//                    "name":"聊友天堂",
//                    "url":"http://www.qq.com/"
//                },
//                {
//                    "name":"菜单",
//                    "sub_button":[
//                        {
//                            "type":"view",
//                            "name":"OAuth认证",
//                            "url":"http://www.weirongm.com/weixin/WeiXin/Oauth.php"
//                        },
//                        {
//                            "type":"view",
//                            "name":"菜单创建",
//                            "url":"http://www.weirongm.com/weixin/WeiXin/Menu.php"
//                        },
//                        {
//                            "type":"view",
//                            "name":"带参二维码",
//                            "url":"http://www.weirongm.com/weixin/WeiXin/QRcode.php"
//                        }
//                    ]
//                }
//            ]
//        }';

        require C('PUBLIC').'/Public/data/MenuWeixin.class.php';
        $Menu = new \data\Menu\Menu();
        $Menu->CreateMenu($menu,C('WECHAT_APPID'),C('WECHAT_APPSECRET'));
        //if(!IS_AJAX) $this->error('请提交');
        /*
        $menuList = array(
            array('id'=>'1','pid'=>'','name'=>I('leftName'),'type'=>I('leftSelect'),'code'=>I('leftView')),
            array('id'=>'2','pid'=>'','name'=>I('centerName'),'type'=>I('centerSelect'),'code'=>I('centerView')),
            array('id'=>'3','pid'=>'','name'=>I('rightName'),'type'=>I('rightSelect'),'code'=>I('rightView')),
            array('id'=>'4','pid'=>'1','name'=>I('leftOneName'),'type'=>I('leftOneSelect'),'code'=>I('leftOneView')),
            array('id'=>'5','pid'=>'1','name'=>I('leftTwoName'),'type'=>I('leftTwoSelect'),'code'=>I('leftTwoView')),
            array('id'=>'7','pid'=>'1','name'=>I('leftThereName'),'type'=>I('leftThereSelect'),'code'=>I('leftThereView')),
            array('id'=>'8','pid'=>'1','name'=>I('leftFourName'),'type'=>I('leftFourSelect'),'code'=>I('leftFourView')),
            array('id'=>'9','pid'=>'1','name'=>I('leftFiveName'),'type'=>I('leftFiveSelect'),'code'=>I('leftFiveView')),
            array('id'=>'10','pid'=>'2','name'=>I('centerOneName'),'type'=>I('centerOneSelect'),'code'=>I('centerOneView')),
            array('id'=>'11','pid'=>'2','name'=>I('centerTwoName'),'type'=>I('centerTwoSelect'),'code'=>I('centerTwoView')),
            array('id'=>'12','pid'=>'2','name'=>I('centerThereName'),'type'=>I('centerThereSelect'),'code'=>I('centerThereView')),
            array('id'=>'13','pid'=>'2','name'=>I('centerFourName'),'type'=>I('centerFourSelect'),'code'=>I('centerFourView')),
            array('id'=>'14','pid'=>'2','name'=>I('centerFiveName'),'type'=>I('centerFiveSelect'),'code'=>I('centerFiveView')),
            array('id'=>'15','pid'=>'3','name'=>I('rightOneName'),'type'=>I('rightOneSelect'),'code'=>I('rightOneView')),
            array('id'=>'16','pid'=>'3','name'=>I('rightTwoName'),'type'=>I('rightTwoSelect'),'code'=>I('rightTwoView')),
            array('id'=>'17','pid'=>'3','name'=>I('rightThereName'),'type'=>I('rightThereSelect'),'code'=>I('rightThereView')),
            array('id'=>'18','pid'=>'3','name'=>I('rightFourName'),'type'=>I('rightFourSelect'),'code'=>I('rightFourView')),
            array('id'=>'19','pid'=>'3','name'=>I('rightFiveName'),'type'=>I('rightFiveSelect'),'code'=>I('rightFiveView')),
        );
        $newMenu = '';
        foreach($menuList as $key){
            if($key['name'] != ''){
                $newMenu[] = $key;
            }
        }
        $menu = Menu::setMenu($newMenu);
        if($menu){
            $this->ajaxReturn('1','EVAL');
        } else{
            $this->ajaxReturn('0','EVAL');
        }
        */
    }
    public function delMenu(){
        $menu = Menu::delMenu();
        dump($menu);
    }
    public function getFans(){
        $fans = UserManage::getFansList($next_openId='');
        dump($fans);
    }
    public function qrCode(){
        header('Content-type: image/jpg');
        $s_id = I('s_id');
        $id = Popularize::createTicket(1,7200,$s_id);
        $qrCode = Popularize::getQrcode($id['ticket'],'');
        echo $qrCode;
    }
}
